import request from '@/utils/request'

// 模拟字典数据
const mockDictData = {
  'certificate_type': [
    { dictCode: 1, dictLabel: '质量证明书', dictValue: 'quality_certificate', cssClass: '', listClass: 'info', isDefault: 'Y', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '质量证明书' },
    { dictCode: 2, dictLabel: '合格证', dictValue: 'conformity_certificate', cssClass: '', listClass: 'success', isDefault: 'N', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '合格证' },
    { dictCode: 3, dictLabel: '检验报告', dictValue: 'inspection_report', cssClass: '', listClass: 'warning', isDefault: 'N', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '检验报告' }
  ],
  'product_category': [
    { dictCode: 4, dictLabel: '管道阀门', dictValue: '1', cssClass: '', listClass: 'primary', isDefault: 'Y', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '管道阀门类别' },
    { dictCode: 5, dictLabel: '管道接头', dictValue: '2', cssClass: '', listClass: 'success', isDefault: 'N', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '管道接头类别' },
    { dictCode: 6, dictLabel: '管道连接器', dictValue: '3', cssClass: '', listClass: 'info', isDefault: 'N', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '管道连接器类别' }
  ],
  'product_grade': [
    { dictCode: 7, dictLabel: 'A级', dictValue: 'A', cssClass: '', listClass: 'success', isDefault: 'Y', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '产品A级' },
    { dictCode: 8, dictLabel: 'B级', dictValue: 'B', cssClass: '', listClass: 'primary', isDefault: 'N', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '产品B级' },
    { dictCode: 9, dictLabel: 'C级', dictValue: 'C', cssClass: '', listClass: 'warning', isDefault: 'N', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '产品C级' }
  ],
  'sys_normal_disable': [
    { dictCode: 10, dictLabel: '正常', dictValue: '0', cssClass: '', listClass: 'primary', isDefault: 'Y', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '正常状态' },
    { dictCode: 11, dictLabel: '停用', dictValue: '1', cssClass: '', listClass: 'danger', isDefault: 'N', status: '0', createBy: 'admin', createTime: '2023-09-01', remark: '停用状态' }
  ]
};

// 使用模拟数据响应
const useMockData = true;

// 查询字典数据列表
export function listData(query) {
  if (useMockData && query.dictType && mockDictData[query.dictType]) {
    console.log('Mock API: listData', query);
    return Promise.resolve({
      code: 200,
      msg: 'success',
      total: mockDictData[query.dictType].length,
      rows: mockDictData[query.dictType]
    });
  }
  
  return request({
    url: '/system/dict/data/list',
    method: 'get',
    params: query
  })
}

// 查询字典数据详细
export function getData(dictCode) {
  if (useMockData) {
    console.log('Mock API: getData', dictCode);
    // 查找所有字典类型中匹配的dictCode
    for (const type in mockDictData) {
      const item = mockDictData[type].find(item => item.dictCode === parseInt(dictCode));
      if (item) {
        return Promise.resolve({
          code: 200,
          msg: 'success',
          data: item
        });
      }
    }
  }
  
  return request({
    url: '/system/dict/data/' + dictCode,
    method: 'get'
  })
}

// 根据字典类型查询字典数据信息
export function getDicts(dictType) {
  if (useMockData && mockDictData[dictType]) {
    console.log('Mock API: getDicts', dictType);
    return Promise.resolve({
      code: 200,
      msg: 'success',
      data: mockDictData[dictType]
    });
  }
  
  return request({
    url: '/system/dict/data/type/' + dictType,
    method: 'get'
  })
}

// 新增字典数据
export function addData(data) {
  if (useMockData && data.dictType && mockDictData[data.dictType]) {
    console.log('Mock API: addData', data);
    const dictList = mockDictData[data.dictType];
    const newDictCode = Math.max(...dictList.map(item => item.dictCode)) + 1;
    const newItem = {
      ...data,
      dictCode: newDictCode,
      createBy: 'admin',
      createTime: new Date().toISOString().split('T')[0]
    };
    
    dictList.push(newItem);
    
    return Promise.resolve({
      code: 200,
      msg: 'success'
    });
  }
  
  return request({
    url: '/system/dict/data',
    method: 'post',
    data: data
  })
}

// 修改字典数据
export function updateData(data) {
  if (useMockData && data.dictType && mockDictData[data.dictType]) {
    console.log('Mock API: updateData', data);
    const dictList = mockDictData[data.dictType];
    const index = dictList.findIndex(item => item.dictCode === data.dictCode);
    
    if (index !== -1) {
      dictList[index] = { ...dictList[index], ...data };
    }
    
    return Promise.resolve({
      code: 200,
      msg: 'success'
    });
  }
  
  return request({
    url: '/system/dict/data',
    method: 'put',
    data: data
  })
}

// 删除字典数据
export function delData(dictCode) {
  if (useMockData) {
    console.log('Mock API: delData', dictCode);
    // 查找并删除所有字典类型中匹配的dictCode
    for (const type in mockDictData) {
      const index = mockDictData[type].findIndex(item => item.dictCode === parseInt(dictCode));
      if (index !== -1) {
        mockDictData[type].splice(index, 1);
        break;
      }
    }
    
    return Promise.resolve({
      code: 200,
      msg: 'success'
    });
  }
  
  return request({
    url: '/system/dict/data/' + dictCode,
    method: 'delete'
  })
}
